package com.itheima.controller;

import com.itheima.domain.UserInfo;
import com.itheima.vo.UserInfoVo;
import com.itheima.service.UserInfoService;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import java.util.Map;

//运行日志
@Slf4j
@RestController
@RequestMapping(path = "/users")
public class UserInfoController {

    //个人信息详情业务
    @Autowired
    private UserInfoService userInfoService;



    /**
     * 查询用户基本信息
     *
     */

    @GetMapping
    public ResponseEntity userInfo(Long userID, @RequestHeader("Authorization") String token) {
        //1. 校验token
        boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken) {
            return ResponseEntity.status(401).body(null);
        }
        //2. 获取token中的用户信息
        Claims claims = JwtUtils.getClaims(token);
        String id = claims.get("id").toString();

        if (userID == null) {
            userID = Long.valueOf(id);
        }

        UserInfoVo userInfo = userInfoService.findById(userID);

        return ResponseEntity.ok(userInfo);
    }


    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,
                                         @RequestHeader("Authorization") String token) {
        //判断token 是否合法
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken) {
            //不合法 就返回禁止访问
            return ResponseEntity.status(401).body(null);
        }

        //获取id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");

        //设置id
        userInfo.setId(Long.valueOf(id));
        //更新id
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    //更新头像
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws IOException {
        userInfoService.uphread(headPhoto);
        return ResponseEntity.ok(null);
    }

    //更新手机号 {"":""}
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map){
        String phone = map.get("phone").toString();

        userInfoService.updatephone(phone);

        return ResponseEntity.ok(null);
    }

    //统计
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map map=userInfoService.counts();
        return ResponseEntity.ok(map);
    }

/*    @GetMapping("/friends/{type}")
    public ResponseEntity type(@PathVariable("type") String type,
                               @RequestParam(defaultValue = "1") Integer pase,
                               @RequestParam(defaultValue = "10") Integer pagesize,
                               String nickname){
        PageResult pageResult=userInfoService.wait();
        ResponseEntity.ok(pageResult);
    }*/
}
